 /****************************************************************************
 * Copyright (C) 2010
 * by Dimok
 * Modified by Dj_Skual
 * for SaveGame Manager GX
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 ***************************************************************************/
#include "FeaturesSettingsMenu.hpp"
#include "../../Controls/Application.h"
#include "../../DeviceControls/DevicesSwap.h"
#include "../../System/IosLoader.h"
#include "../../Prompts/PromptWindows.h"
#include "../../FileOperations/fileops.h"
#include "../../Network/Updates/UpdateGameTDBTask.h"
#include "../../Menu/Explorer.h"
#include "../../Menu/TaskBar.hpp"

static const char * OnOffText[MAX_ON_OFF] =
{
	trNOOP( "OFF" ),
	trNOOP( "ON" )
};

static const char *ScreenshotTypeText[SCREENSHOT_MAX] =
{
	"PNG",
	"JPEG",
	"GIF",
	"TIFF",
	"BMP",
	"GD",
	"GD2"
};

FeaturesSettingsMenu::FeaturesSettingsMenu()
	: SettingsBrowser()
{
	GameTDBSetting = Settings.GameTDBTitles;
	USBPortSetting = Settings.USBPort;
	EmuBrowsersSetting = Settings.EmuBrowsers;
	
	SetupOptionName();
	show();
}

FeaturesSettingsMenu::~FeaturesSettingsMenu()
{
	bool reload = false;
	
	if (USBPortSetting != Settings.USBPort)
	{
		DevicesSwap::Instance()->SwapUSBPorts();
		
		while(DevicesSwap::Instance()->SwappingUSBPorts())
			usleep(50);
		
		reload = true;
	}
	
	if (GameTDBSetting != Settings.GameTDBTitles &&
		Settings.LoadedBrowser != MII_BROWSER &&
		Settings.LoadedBrowser != EMII_BROWSER)
		reload = true;
	
	if(EmuBrowsersSetting != Settings.EmuBrowsers)
		FixEMiiDB();
	
	if(reload)
	{
		Explorer::Instance()->Reload();
		TaskBar::Instance()->UpdateAllButtons();
	}
}

void FeaturesSettingsMenu::SetupOptionName()
{
	int Idx = 0;
	
	options->SetName(Idx++, tr( "GameTDB Titles" ));
	options->SetName(Idx++, tr( "Screenshot Format" ));
	options->SetName(Idx++, tr( "USB Port(s)" ));
	options->SetName(Idx++, tr( "Priiloader Override" ));
	if(!Settings.SneekRunning)
		options->SetName(Idx++, tr("Emu browsers"));
	
	SetOptionValues();
}

void FeaturesSettingsMenu::SetOptionValues()
{
	int Idx = 0;
	
	//! Settings: GameTDB Titles
	options->SetValue(Idx++, tr(OnOffText[Settings.GameTDBTitles]));
	
	//! Settings: Screenshot Format
	options->SetValue(Idx++, ScreenshotTypeText[Settings.ScreenshotFormat]);
	
	//! Settings: USB Port
	if(Settings.USBPort == 2)
		options->SetValue(Idx++, tr("Use Both"));
	else
		options->SetValue(Idx++, "%i", Settings.USBPort);
	
	//! Settings: PriiLoader Override
	options->SetValue(Idx++, tr(OnOffText[Settings.PriiloaderOverride]));

	//! Settings: Emu Nand browse
	if(!Settings.SneekRunning)
		options->SetValue(Idx++, tr(OnOffText[Settings.EmuBrowsers]));
}

void FeaturesSettingsMenu::OnBrowserClick(GuiOptionBrowser * browser UNUSED, int option)
{
	switch (option)
	{
		case 0:
			if (++Settings.GameTDBTitles >= MAX_ON_OFF) Settings.GameTDBTitles = 0;
			
			if (Settings.GameTDBTitles == 1)
			{
				std::string xmlPath= Settings.GameTDBPath;
				xmlPath += "/wiitdb.xml";
				
				if (!CheckFile(xmlPath.c_str()))
				{
					if(WindowPrompt(tr("GameTDB not found."), tr("Do you want to update now ?"), tr("Yes"), tr("Cancel")))
					{
						UpdateGameTDBTask * task = new UpdateGameTDBTask();
						
						while(task->IsRunning())
							Application::Instance()->updateEvents();
							
						if(task->Succes())
							ShowMsg(fmt(tr("%s successfully updated."), "GameTDB"), 0);
						else
							ShowError(tr("%s update failed."), "GameTDB");
						
						delete task;
					}
				}
			}
			break;
		case 1:
			if (++Settings.ScreenshotFormat >= SCREENSHOT_MAX) Settings.ScreenshotFormat = SCREENSHOT_PNG;
			break;
		case 2:
			if(!IosLoader::IsHermesIOS() && !IosLoader::IsD2X())
				ShowError(tr("USB Port changing is only supported on Hermes and D2X cIOS."));
			else if (++Settings.USBPort > 2) Settings.USBPort = 0;
			break;
		case 3:
			if (++Settings.PriiloaderOverride >= MAX_ON_OFF) Settings.PriiloaderOverride = OFF;
			break;
		case 4:
			if (++Settings.EmuBrowsers >= MAX_ON_OFF) Settings.EmuBrowsers = OFF;
			break;
		default:
			break;
	}
	
	SetOptionValues();
	optionBrowser->Refresh();
}
